#!/bin/bash
# Options
DIR=src/filesystem
FAT=fat
FILES=files
TMPDIR=/tmp/MakeFS
PAGE=01
# colours
if [ "$1" == "" ]; then
	COL='\033[01;38;5;160m'
else
	COL="$1"
fi;
NORM='\033[39m'
# Guts of the parsing
function parsedir {
	D=`cat $TMPDIR/D.$7.$2.tmp`
	P=`cat $TMPDIR/D.$8.$7.tmp`
	echo -e ".echo \"$3Entry $2/: \",$\n\tdb FILE_DIRECTORY\t; Type ID (1 byte)\n\tdb \"$2\",0\t; Directory Name\n\tdw $D\t; Directory ID (2 bytes)\n\tdw $P\t; Parent Directory ID (2 bytes)\n" > $1/$2.asm
	ls $1 | while read ITEM; do
		EXT=`echo $ITEM|awk -F . '{print $NF}'`;
		if test -d $1/$ITEM; then
			echo -e "$COL$3$ITEM/$NORM";
			echo -e "#include \"$1/$ITEM/$ITEM.asm\"\n" >> $4/$5;
			T='\t'$3
			D=`cat $TMPDIR/D.tmp`
			let "D += 1"
			echo $D > $TMPDIR/D.tmp
			echo $D > $TMPDIR/D.$2.$ITEM.tmp
			parsedir $1/$ITEM $ITEM $T $4 $5 $6 $2 $7;
		elif [ $ITEM != $2.asm ] && [ $ITEM != $6 ] && [ $EXT != "log" ] && [ $EXT != "bin" ] && [ $EXT != "lst" ] && [ $EXT != "inc" ] && ! test -f $1/$ITEM.asm; then
			if [ $EXT == "asm" ]; then
				BASE=`basename $ITEM .asm`
				echo -e "$COL$3$BASE$NORM";
				echo -e "$3$COL->$NORM Compiling $ITEM";
				$ROOT/tools/spasm -T -I $ROOT/src/inc/ $1/$ITEM $1/$BASE.bin | awk "{print \"\t$3\", \$0}";
				filename=$(basename $ITEM)
				filename=${filename%.*.*}
				mv $1/$BASE.bin $1/$BASE
				echo -e "$3$COL<-$NORM Done Compiling $ITEM";
				echo -e ".echo \"\t$3Entry $ITEM: \",$\n\tdb FILE_FILE\t; Type ID (1 byte)\n\tdb \"$BASE\",0\t; File Name\n\tdb 01h\t; Flash Page ( 1 byte)\n\tdw $BASE\t; Pointer to Data (2 bytes)\n\tdw $BASE.end-$BASE\t; Length Of Data (2 bytes)\n\tdw $D\t; Parent Directory ID (2 bytes)\n" >> $1/$2.asm;
				echo -e ".echo \"$ITEM Start \",NORMAL,$,COLOUR,\" Length \",NORMAL,$BASE.end-$BASE,COLOUR\n$BASE:\n\tdw $BASE.end-$BASE\n\t#import \"$1/$BASE\"\n$BASE.end:\n" >> $4/$6;
			else
				echo -e "$COL$3$ITEM$NORM";
				echo -e ".echo \"\t$3Entry $ITEM: \",$\n\tdb FILE_FILE\t; Type ID (1 byte)\n\tdb \"$ITEM\",0\t; File Name\n\tdb 01h\t; Flash Page ( 1 byte)\n\tdw $ITEM\t; Pointer to Data (2 bytes)\n\tdw $ITEM.end-$ITEM\t; Length Of Data (2 bytes)\n\tdw $D\t; Parent Directory ID (2 bytes)\n" >> $1/$2.asm;
				echo -e ".echo \"$ITEM Start \",NORMAL,$,COLOUR,\" Length \",NORMAL,$ITEM.end-$ITEM,COLOUR\n$ITEM:\n\tdw $ITEM.end-$ITEM\n\t#import \"$1/$ITEM\"\n.seek \$-1\t;Remove the extra newline appended by text editors. Will have to figure out how to handle pre-compiled binaries etc later\n\tdb 0\n$ITEM.end:\n" >> $4/$6;
			fi;
		fi;
	done;
}
# Setup
mkdir -p $TMPDIR/
echo "0" > $TMPDIR/D.$FAT.$FAT.tmp
echo "0" > $TMPDIR/D.tmp
echo "; File Data" > $DIR/$FILES.asm
ROOT=`pwd`
cd $DIR
# Parsing
parsedir $ROOT/$DIR $FAT '\t' $ROOT/$DIR $FAT.asm $FILES.asm $FAT $FAT
cd $ROOT;
echo -e ".echo \"Start of FAT: \",NORMAL,$\nFATTable: ;Start of the FAT\n\tdw \$ ;FILE_START\n"|cat - $DIR/$FAT.asm > $TMPDIR/out && mv $TMPDIR/out $DIR/$FAT.asm
echo -e ".echo COLOUR,\"End of FAT \",NORMAL,$,COLOUR\n\tdb FILE_END\t; End of the FAT" >> $DIR/$FAT.asm
echo -e ".org 04000h\n.echo \"Compiling Page\",NORMAL,\" 01\",COLOUR\n\t#include ../filesystem/$FAT.asm\n\t#include ../filesystem/$FILES.asm\n.echo \"Free Space: \",NORMAL,08000h - $,\" bytes\",COLOUR\n\tblock 08000h - $" > src/pages/$PAGE/$PAGE.asm
# Cleanup
rm -rf $TMPDIR/*
